package net.itgoo.parkingpay.ui.person;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AlertDialog;

import net.itgoo.parkingpay.R;
import net.itgoo.parkingpay.ui.signin.ParkingSigninActivity;
import net.itgoo.parkingpay.vendor.widget.activity.ParkingBaseActivity;
import net.itgoo.parkingpay.vendor.widget.fragment.ParkingBaseFragment;
import net.itgoo.titlebar.TitleBar;

import hk.ids.gws.android.sclick.SClick;

public class ParkingPersonFragment extends ParkingBaseFragment implements ParkingPersonContract.View {
    private ParkingPersonContract.Presenter mPresenter;
    private TitleBar mTitleBar;

    public ParkingPersonFragment() {
        // Required empty public constructor
    }

    public static ParkingPersonFragment newInstance(Bundle bundle) {
        ParkingPersonFragment fragment = new ParkingPersonFragment();
        if (bundle != null) {
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mPresenter != null) {
            mPresenter.start();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.stop();
        }
    }

    @Override
    public void setPresenter(ParkingPersonContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    protected int getLayoutResourceID() {
        return R.layout.parking_fragment_person;
    }

    @Override
    protected void initUI(Bundle savedInstanceState) {
        initViews();
        initTitleBar();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    private void initViews() {
        View contentView = getContentView();
        mTitleBar = contentView.findViewById(R.id.parking_view_title_bar_ct);
        contentView.findViewById(R.id.parking_fragment_person_logout_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!SClick.check(SClick.BUTTON_CLICK)) return;

                new AlertDialog.Builder(getActivity())
                        .setMessage(R.string.parking_logout_confirm)
                        .setPositiveButton(R.string.parking_ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                if (!SClick.check(SClick.BUTTON_CLICK)) return;

                                mPresenter.logout();
                            }
                        }).setNegativeButton(R.string.parking_cancel, null)
                        .create().show();
            }
        });
    }

    private void initTitleBar() {
        mTitleBar.setTitle(getString(R.string.parking_pay));
        mTitleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!SClick.check(SClick.BUTTON_CLICK)) return;
                getActivity().finish();
            }
        });
    }

    @Override
    public void showLogout() {
        ((ParkingBaseActivity) (getActivity())).finishToActivity(ParkingSigninActivity.class);
    }
}
